package org.graduates.webapp.actions.post;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.graduates.webapp.actions.BaseAction;
import org.projects.graduates.domain.Person;
import org.projects.graduates.domain.Post;
import org.projects.graduates.domain.PostReply;
import org.projects.graduates.domain.TimeTable;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("web")
public class CommitPostAction extends BaseAction {

	private static final long serialVersionUID = -5689512076341718757L;

	// 用于提交成功之路以后返回给前端必须的数据
	private List<Post> posteds = new ArrayList<Post>();

	private Post post;

	private long tid = 0L;

	/**
	 * **************************这里需要优化
	 */

	@Action(results = { @Result(name = "success", type = "json", params = {
			"includeProperties",
			"cPerson,cPerson.showName, cPerson,id,"
					+ "posteds,posteds\\[\\d+\\]\\.id,posteds\\[\\d+\\]\\.upDate,posteds\\[\\d+\\]\\.content"
					+ ",posteds\\[\\d+\\]\\.who, posteds\\[\\d+\\]\\.who.id,posteds\\[\\d+\\]\\.who.showName, " }) })
	public String execute() {

		if (0L == tid || null == post) {
			return ActionSupport.INPUT;
		}

		TimeTable tt = TimeTable.get(TimeTable.class, tid);

		if (null == tt) {
			return ActionSupport.INPUT;
		}

		post.setWho(getCurrentPerson());
		post.setUpDate(new Date());
		this.gradApplication.save(post);

		PostReply postReply = new PostReply(tt, post);
		this.gradApplication.save(postReply);

		posteds.add(post);

		return ActionSupport.SUCCESS;
	}

	@JSON(serialize = false)
	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}

	public long getTid() {
		return tid;
	}

	public void setTid(long tid) {
		this.tid = tid;
	}

	public List<Post> getPosteds() {
		return posteds;
	}

	public void setPosteds(List<Post> posteds) {
		this.posteds = posteds;
	}

	public Person getcPerson() {
		return getCurrentPerson();
	}


}
